package com.jobsleaf.viewsorter.tree;

import java.util.List;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;

import com.jobsleaf.tree.domain.Gsm;
import com.jobsleaf.tree.domain.Lac;
import com.jobsleaf.tree.domain.Rnc;
import com.jobsleaf.tree.domain.Subnetwork;
import com.jobsleaf.tree.domain.Utran;

public class SubnetworkContentProvider implements ITreeContentProvider
{
    private static Object[] EMPTY_ARRAY = new Object[0];
    protected TreeViewer mViewer;

    public void dispose()
    {
    }

    public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
    {
	mViewer = (TreeViewer) viewer;

    }

    public Object[] getChildren(Object parentElement)
    {
	if (parentElement instanceof Subnetwork)
	{
	    return ((Subnetwork) parentElement).getChildren().toArray();
	}

	if (parentElement instanceof Utran)
	{
	    return ((Utran) parentElement).getChildren().toArray();
	}
	if (parentElement instanceof Gsm)
	{
	    return ((Gsm) parentElement).getChildren().toArray();
	}
	return EMPTY_ARRAY;
    }

    public Object getParent(Object element)
    {
	if (element instanceof Rnc)
	{
	    return ((Rnc) element).getParent();
	}
	else if (element instanceof Lac)
	{
	    return ((Lac) element).getParent();
	}
	else if (element instanceof Utran)
	{
	    return ((Utran) element).getParent();
	}
	else if (element instanceof Gsm)
	{
	    return ((Gsm) element).getParent();
	}

	return null;
    }

    public boolean hasChildren(Object element)
    {
	if (element instanceof Subnetwork)
	{
	    return ((Subnetwork) element).getChildren().size() > 0;
	}
	if (element instanceof Utran)
	{
	    return ((Utran) element).getChildren().size() > 0;
	}
	if (element instanceof Gsm)
	{
	    return ((Gsm) element).getChildren().size() > 0;
	}
	return false;
    }

    public Object[] getElements(Object inputElement)
    {
	return ((List<Object>) inputElement).toArray();
    }
}
